<template>
  <div class="my-love-song-container">
    <!-- 歌单信息 -->
    <div class="playlist-info">
      <!-- 歌单封面 -->
      <el-image style="width: 182px; height: 182px; border-radius: 6px" v-if="myLoveSongList[0].album.picUrl !== undefined" :src="myLoveSongList[0].album.picUrl" alt=""></el-image>
      <!-- 歌单名称 -->
      <div class="name-box">
        <span class="type">歌单</span>
        <h2>我喜欢的音乐</h2>
      </div>
      <!-- 播放全部 -->
      <div class="play-all" @click="open">
        <i class="el-icon-caret-right"></i>
        <span>播放全部</span>
        <i class="el-icon-plus"></i>
      </div>
      <!-- 其他 -->
      <div class="other-msg">
        <i>歌曲 : {{ myLoveSongList.length }}</i>
      </div>
    </div>
    <!-- 歌曲列表 -->
    <div class="playlist-songlist">
      <el-table :data="myLoveSongList" stripe style="width: 100%">
        <el-table-column type="index" width="50" align="center"></el-table-column>
        <el-table-column prop="delete" label="操作" min-width="5%">
          <template slot-scope="scope">
            <i class="el-icon-star-on" @click="deleteMyLoveSongList(scope.row)"></i>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="标题" min-width="40%">
          <template slot-scope="scope">
            <span style="display: inline-block; width: 100%" @click="chooseSong(scope.row.id)">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="artist" label="歌手" min-width="20%" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-for="item in scope.row.artist" :key="item.id">{{ item.name }}{{ item.name !== scope.row.artist[scope.row.artist.length - 1].name ? ' / ' : '' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="album" label="专辑" min-width="20%">
          <template slot-scope="scope">
            <span style="cursor: pointer" @click="goAlbum(scope.row.album.id)">{{ scope.row.album.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="time" label="时间" min-width="15%">
          <template slot-scope="scope">{{ scope.row.time | conversion('mm : ss') }}</template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import { savePlayList, clearPlayList, loadMyLoveSongList, deleteMyLoveSongList } from '@/js/cache'

import bus from '@/components/eventBus.js'

export default {
  name: 'MyLoveSong',
  data() {
    return {
      myLoveSongList: []
    }
  },
  created() {
    this.myLoveSongList = loadMyLoveSongList()
    bus.$on('initLoveState', () => {
      this.myLoveSongList = loadMyLoveSongList()
    })
  },
  methods: {
    deleteMyLoveSongList(row) {
      deleteMyLoveSongList(row)
      this.myLoveSongList = loadMyLoveSongList()
      // 重新渲染 class
      bus.$emit('initLoveState')
    },
    open() {
      this.$confirm('"播放全部"将会替换当前的播放列表，是否继续？', '替换播放列表', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        lockScroll: false
      })
        .then(() => {
          // 执行替换当前的播放列表的操作
          // 清空当前播放列表
          clearPlayList()
          // 准备新的列表数据，存入播放列表中
          this.myLoveSongList.forEach((item) => {
            let obj = {
              id: item.id,
              name: item.name,
              artist: item.artist[0].name,
              time: item.time
            }
            savePlayList(obj)
          })
          // 重新渲染播放列表数据
          bus.$emit('initPlayList')
        })
        .catch(() => {
          // console.log('取消')
        })
    },
    chooseSong(id) {
      bus.$emit('songId', id)
    },
    goAlbum(id) {
      this.$router.push({
        path: '/album',
        query: {
          id: id
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.my-love-song-container {
  .playlist-info {
    .name-box {
      text-align: left;
      padding-top: 30px;
      .type {
        padding: 0 2px;
        font-size: 14px;
        color: #ec4141;
        border: 1px solid #ec4141;
        border-radius: 3px;
      }
      h2 {
        display: inline-block;
        margin-left: 10px;
        font-size: 20.5px;
      }
    }
    .el-image {
      float: left;
      margin: 30px;
    }
    .play-all {
      position: relative;
      margin-top: 44.5px;
      margin-left: 242px;
      width: 140px;
      height: 32.5px;
      line-height: 32.5px;
      background-color: #ec4141;
      text-align: center;
      border-radius: 16.25px;
      cursor: pointer;
      &:hover {
        background-color: #cc3232;
      }
      .el-icon-caret-right {
        position: absolute;
        top: 50%;
        margin-top: -12px;
        left: 10px;
        font-size: 24px;
        color: #fff;
      }
      span {
        margin: 0;
        font-size: 17px;
        color: #fff;
      }
      .el-icon-plus {
        position: absolute;
        top: 50%;
        margin-top: -8px;
        right: 10px;
        font-size: 16px;
        color: #fef4f4;
      }
    }
    .other-msg {
      overflow: hidden;
      margin-top: 39.2px;
      i {
        float: left;
        margin-right: 10px;
        font-size: 14px;
      }
    }
  }
  // 歌曲列表
  .playlist-songlist {
    /deep/.el-table__cell {
      padding: 9px 0;
      font-size: 12px;
    }
    .el-icon-star-on {
      font-size: 20px;
      color: #ec4141;
      cursor: pointer;
    }
  }
}
</style>
